package com.exam.auth.controller;

import com.exam.auth.config.JwtProperties;
import com.exam.auth.entiy.UserInfo;
import com.exam.auth.service.AuthService;
import com.exam.auth.utils.JwtUtils;
import com.exam.common.entity.Request;
import com.exam.common.until.CookieUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName : AuthController
 * @Description : 授权中心
 * @Author : WHB
 */

@RestController
@Api(description = "授权中心API接口")
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController {
    @Autowired
    private JwtProperties prop;
    @Autowired
    private AuthService authService;

    @Value("${exam.jwt.cookieName}")
    private String cookieName;

//    @PostMapping("login")
//    @ApiOperation(value="登录", notes="登录")
//    public Request login(@RequestParam("username") String username,
//                                      @RequestParam("password") String password,
//                                      HttpServletRequest request,
//                                      HttpServletResponse response) {
//            //登录
//            String token = this.authService.login(username, password);
//            if (StringUtils.isBlank(token)) {
//                System.out.println("没有token");
//                return new Request(false,400,"登录失败");
//            }
//           CookieUtils.setCookie(request,response,cookieName,token,60*60,false);
//        return new Request(true,200,"登录成功");
//    }

    @PostMapping("login")
    @ApiOperation(value="登录", notes="登录")
    public Request login(@RequestBody Map<String,String> data,
                         HttpServletRequest request,
                         HttpServletResponse response) {
        String username = data.get("username");
        String password = data.get("password");

        //登录
        String token = this.authService.login(username, password);
        if (StringUtils.isBlank(token)) {
            System.out.println("没有token");
            return new Request(false,400,"登录失败");
        }
        CookieUtils.setCookie(request,response,cookieName,token,60*60*60,false);
        Map<String,Object> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        return new Request(true,20000,"登录成功",tokenMap);
    }

    @GetMapping("info")
    public Request getInfo(@RequestParam("token") String token){
        try {
            //解析token
            UserInfo info = JwtUtils.getInfoFromToken(token,prop.getPublicKey());
            return new Request(true,20000,"已登录",info);
        }catch (Exception e){
            return new Request(false,202,"token已过期");
        }
    }

    @GetMapping("verify")
    @ApiOperation(value="认证", notes="认证")
    public Request verify(@CookieValue(value = "EXAM_TOKEN",required = false) String token ,
                          HttpServletRequest request,
                          HttpServletResponse response) throws Exception{
        if (StringUtils.isBlank(token)){
            //没有token
            return new Request(false,201,"未登录");
        }
        try {
            //解析token
            UserInfo info = JwtUtils.getInfoFromToken(token,prop.getPublicKey());
            //刷新token
            JwtUtils.generateToken(info,prop.getPrivateKey(),prop.getExpire());
            CookieUtils.setCookie(request,response,cookieName,token,30,false);
            return new Request(true,200,"已登录",info);
        }catch (Exception e){
            return new Request(false,202,"token已过期");
        }
    }
}
